﻿using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Input;

namespace ThunderboltCustomControlLibrary;

public class SearchTextBox:TextBox
{
	static SearchTextBox()
	{
		DefaultStyleKeyProperty.OverrideMetadata(typeof(SearchTextBox), new FrameworkPropertyMetadata(typeof(SearchTextBox)));
	}

	[CategoryAttribute("Custom attribute"), DescriptionAttribute("Gets or sets the default text")]
	public string PlaceHolder
	{
		get { return (string)GetValue(PlaceHolderProperty); }
		set { SetValue(PlaceHolderProperty, value); }
	}
	[CategoryAttribute("Custom attribute"), DescriptionAttribute("Gets or sets the default text")]
	public static readonly DependencyProperty PlaceHolderProperty = DependencyProperty.Register("PlaceHolder", typeof(string), typeof(SearchTextBox));

	public override void OnApplyTemplate()
	{
		base.OnApplyTemplate();

		ButtonBase clearBtn = this.Template.FindName("PART_ContentHostClearButton", this) as ButtonBase;
		ButtonBase searchBtn = this.Template.FindName("PART_ContentHostSearchButton", this) as ButtonBase;
		TextBlock placeholderText = this.Template.FindName("PART_ContentHostPlaceText", this) as TextBlock;
		placeholderText.Visibility = Visibility.Visible;
		searchBtn.Visibility = Visibility.Visible;
		clearBtn.Visibility = Visibility.Collapsed;
		//this.Text = PlaceHolder;
		this.Opacity = 0.4;

		clearBtn.Click += (s, e) =>
		{
			this.Text = "";
			//placeholderText.Visibility=Visibility.Visible;
		};

		this.LostFocus += (s, e) =>
		{
			if (this.Text.Length == 0)
			{
				//this.Text = PlaceHolder;
				this.Opacity = 0.4;
				placeholderText.Visibility = Visibility.Visible;
				searchBtn.Visibility = Visibility.Visible;
				clearBtn.Visibility = Visibility.Collapsed;
			}
			else
			{
				placeholderText.Visibility = Visibility.Collapsed;
				clearBtn.Visibility = Visibility.Visible;
				searchBtn.Visibility = Visibility.Collapsed;
			}
		};

		this.GotFocus += (s, e) =>
		{
			if (this.Text == "")
			{
				placeholderText.Visibility = Visibility.Visible;
			}
			clearBtn.Visibility = Visibility.Visible;
			searchBtn.Visibility = Visibility.Collapsed;
		};
	}

	protected override void OnTextChanged(TextChangedEventArgs e)
	{
		//if (this.Text != PlaceHolder)
		//{
			base.OnTextChanged(e);
		//}

		//if (this.Text.EndsWith(PlaceHolder))
		//{
		//	this.Text = e.ToString();
		//}
		if (this.Template != null)
		{
			ButtonBase clearBtn = this.Template.FindName("PART_ContentHostClearButton", this) as ButtonBase;
			ButtonBase searchBtn = this.Template.FindName("PART_ContentHostSearchButton", this) as ButtonBase;
		TextBlock placeholderText = this.Template.FindName("PART_ContentHostPlaceText", this) as TextBlock;

		this.Opacity = 1;

			if (this.Text.Length > 0)
			{
				if (this.Text != PlaceHolder)
				{
					placeholderText.Visibility = Visibility.Collapsed;
					clearBtn.Visibility = Visibility.Visible;
					searchBtn.Visibility = Visibility.Collapsed;
				}
				else
				{
					this.Opacity = 0.4;
					placeholderText.Visibility = Visibility.Visible;
					searchBtn.Visibility = Visibility.Visible;
					clearBtn.Visibility = Visibility.Collapsed;
				}
			}
			else
			{
				if (this.IsFocused)
				{
					placeholderText.Visibility = Visibility.Visible;
					clearBtn.Visibility = Visibility.Visible;
					searchBtn.Visibility = Visibility.Collapsed;
				}
				else
				{
					//this.Text = PlaceHolder;
				}
			}
		}
	}

	protected override void OnPreviewTextInput(TextCompositionEventArgs e)
	{
		if (this.Text.EndsWith(PlaceHolder))
		{
			this.Text =e.Text;
		}
		
	}
}
